import JsonP from 'jsonp'
import axios from 'axios'
import {Toast} from 'antd-mobile'
import {CONFIG} from '../config/index'
export default class Axios{
    static jsonp(options){
        return new Promise((resolve,reject)=>{
            JsonP(options.url,{
                param:'callback'
            },function(err,response){
                if (response.status ==='success') {
                    resolve(response);
                } else {
                    reject(response.messsage);
                }
            })
        })
    }

    static ajax(options){
        let baseApi=CONFIG.SERVER_BASE_URL;
        let token=window.localStorage.accessToken || '';
        return new Promise((resolve,reject)=>{
            axios({
                url:options.url,
                method:options.method,
                baseURL:baseApi,
                timeout:10000,
                headers:{
                    "Authorization":"Bearer " + token
                },            
                params:options.data||''
            }).then((response)=>{
                if(response.status===200){
                    resolve(response.data);
                }
            }).catch((err)=>{
                let res=err.response;
                reject(res.data);              
                console.log('resErr:',res.data);  
            });
        })
    }
}
